﻿namespace REVISIONMAX20
{
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;

    public class frmSQLItem : Form
    {
        private UIHandler _uiHandler = null;
        private ToolStripButton btnApply;
        private Button btnBrowse;
        private ToolStripButton btnExpire;
        private ToolStripButton btnSave;
        private IContainer components = null;
        private FolderBrowserDialog folderBrowserDialog1;
        private Label label1;
        private Label label2;
        private Label label3;
        private Label label4;
        private Label label5;
        private Label label6;
        private Label label7;
        private Label label8;
        private Label label9;
        private MyTextBox myTextBox1;
        private SQLItem sqlitem = null;
        private ToolStripSeparator toolStripSeparator1;
        private ToolStripSeparator toolStripSeparator3;
        private ToolStripSeparator toolStripSeparator4;
        private ToolStrip tsMain;
        private MyTextBox txtExecuted;
        private MyTextBox txtExpired;
        private MyTextBox txtIndex;
        private MyTextBox txtRevision;
        private MyTextBox txtSource;
        private ComboBox cboSourceType;
        private ComboBox cboConnectionStrings;
        private MyTextBox txtSourceText;

        public frmSQLItem()
        {
            this.InitializeComponent();
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            this.Save(true);
        }

        private void btnExpire_Click(object sender, EventArgs e)
        {
            DateTime? expired = this.sqlitem.expired;
            if (!expired.HasValue)
            {
                this.sqlitem.expired = new DateTime?(DateTime.Now);
            }
            else
            {
                this.sqlitem.expired = null;
            }
            this.UpdateViewer();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            this.Save(false);
        }

        private void cboSourceType_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();
            this.btnBrowse = new System.Windows.Forms.Button();
            this.toolStripSeparator4 = new System.Windows.Forms.ToolStripSeparator();
            this.tsMain = new System.Windows.Forms.ToolStrip();
            this.btnSave = new System.Windows.Forms.ToolStripButton();
            this.btnApply = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator3 = new System.Windows.Forms.ToolStripSeparator();
            this.btnExpire = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
            this.label9 = new System.Windows.Forms.Label();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.label5 = new System.Windows.Forms.Label();
            this.label6 = new System.Windows.Forms.Label();
            this.label7 = new System.Windows.Forms.Label();
            this.label8 = new System.Windows.Forms.Label();
            this.txtRevision = new REVISIONMAX20.MyTextBox(this.components);
            this.txtIndex = new REVISIONMAX20.MyTextBox(this.components);
            this.txtExpired = new REVISIONMAX20.MyTextBox(this.components);
            this.myTextBox1 = new REVISIONMAX20.MyTextBox(this.components);
            this.txtExecuted = new REVISIONMAX20.MyTextBox(this.components);
            this.txtSourceText = new REVISIONMAX20.MyTextBox(this.components);
            this.txtSource = new REVISIONMAX20.MyTextBox(this.components);
            this.cboSourceType = new System.Windows.Forms.ComboBox();
            this.cboConnectionStrings = new System.Windows.Forms.ComboBox();
            this.tsMain.SuspendLayout();
            this.SuspendLayout();
            // 
            // btnBrowse
            // 
            this.btnBrowse.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.btnBrowse.Location = new System.Drawing.Point(620, 188);
            this.btnBrowse.Name = "btnBrowse";
            this.btnBrowse.Size = new System.Drawing.Size(25, 20);
            this.btnBrowse.TabIndex = 38;
            this.btnBrowse.Text = "...";
            this.btnBrowse.UseVisualStyleBackColor = true;
            // 
            // toolStripSeparator4
            // 
            this.toolStripSeparator4.Name = "toolStripSeparator4";
            this.toolStripSeparator4.Size = new System.Drawing.Size(6, 25);
            // 
            // tsMain
            // 
            this.tsMain.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.btnSave,
            this.toolStripSeparator4,
            this.btnApply,
            this.toolStripSeparator3,
            this.btnExpire,
            this.toolStripSeparator1});
            this.tsMain.Location = new System.Drawing.Point(0, 0);
            this.tsMain.Name = "tsMain";
            this.tsMain.Size = new System.Drawing.Size(657, 25);
            this.tsMain.TabIndex = 37;
            this.tsMain.Text = "toolStrip1";
            // 
            // btnSave
            // 
            this.btnSave.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.btnSave.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnSave.Name = "btnSave";
            this.btnSave.Size = new System.Drawing.Size(35, 22);
            this.btnSave.Text = "Save";
            this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
            // 
            // btnApply
            // 
            this.btnApply.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.btnApply.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnApply.Name = "btnApply";
            this.btnApply.Size = new System.Drawing.Size(38, 22);
            this.btnApply.Text = "Apply";
            this.btnApply.Click += new System.EventHandler(this.btnApply_Click);
            // 
            // toolStripSeparator3
            // 
            this.toolStripSeparator3.Name = "toolStripSeparator3";
            this.toolStripSeparator3.Size = new System.Drawing.Size(6, 25);
            // 
            // btnExpire
            // 
            this.btnExpire.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.btnExpire.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnExpire.Name = "btnExpire";
            this.btnExpire.Size = new System.Drawing.Size(41, 22);
            this.btnExpire.Text = "Expire";
            this.btnExpire.Click += new System.EventHandler(this.btnExpire_Click);
            // 
            // toolStripSeparator1
            // 
            this.toolStripSeparator1.Name = "toolStripSeparator1";
            this.toolStripSeparator1.Size = new System.Drawing.Size(6, 25);
            // 
            // label9
            // 
            this.label9.Location = new System.Drawing.Point(1, 192);
            this.label9.Name = "label9";
            this.label9.Size = new System.Drawing.Size(80, 20);
            this.label9.TabIndex = 35;
            this.label9.Text = "Source";
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(1, 139);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(80, 20);
            this.label1.TabIndex = 35;
            this.label1.Text = "Source Type";
            // 
            // label2
            // 
            this.label2.Location = new System.Drawing.Point(1, 88);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(80, 20);
            this.label2.TabIndex = 35;
            this.label2.Text = "Index";
            // 
            // label3
            // 
            this.label3.Location = new System.Drawing.Point(1, 114);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(80, 20);
            this.label3.TabIndex = 35;
            this.label3.Text = "Revision";
            // 
            // label4
            // 
            this.label4.Location = new System.Drawing.Point(1, 166);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(91, 20);
            this.label4.TabIndex = 35;
            this.label4.Text = "Connection String";
            // 
            // label5
            // 
            this.label5.Location = new System.Drawing.Point(1, 37);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(80, 20);
            this.label5.TabIndex = 35;
            this.label5.Text = "Executed";
            // 
            // label6
            // 
            this.label6.Location = new System.Drawing.Point(1, 38);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(80, 20);
            this.label6.TabIndex = 35;
            this.label6.Text = "Executed";
            // 
            // label7
            // 
            this.label7.Location = new System.Drawing.Point(1, 62);
            this.label7.Name = "label7";
            this.label7.Size = new System.Drawing.Size(80, 20);
            this.label7.TabIndex = 35;
            this.label7.Text = "Expired";
            // 
            // label8
            // 
            this.label8.Location = new System.Drawing.Point(1, 216);
            this.label8.Name = "label8";
            this.label8.Size = new System.Drawing.Size(80, 20);
            this.label8.TabIndex = 35;
            this.label8.Text = "Source Text";
            // 
            // txtRevision
            // 
            this.txtRevision.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.txtRevision.ForeColor = System.Drawing.Color.Blue;
            this.txtRevision.Location = new System.Drawing.Point(98, 114);
            this.txtRevision.Name = "txtRevision";
            this.txtRevision.Size = new System.Drawing.Size(55, 20);
            this.txtRevision.TabIndex = 33;
            // 
            // txtIndex
            // 
            this.txtIndex.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.txtIndex.ForeColor = System.Drawing.Color.Blue;
            this.txtIndex.Location = new System.Drawing.Point(98, 88);
            this.txtIndex.Name = "txtIndex";
            this.txtIndex.Size = new System.Drawing.Size(55, 20);
            this.txtIndex.TabIndex = 33;
            // 
            // txtExpired
            // 
            this.txtExpired.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.txtExpired.ForeColor = System.Drawing.Color.Blue;
            this.txtExpired.Location = new System.Drawing.Point(98, 62);
            this.txtExpired.Name = "txtExpired";
            this.txtExpired.ReadOnly = true;
            this.txtExpired.Size = new System.Drawing.Size(132, 20);
            this.txtExpired.TabIndex = 33;
            // 
            // myTextBox1
            // 
            this.myTextBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.myTextBox1.ForeColor = System.Drawing.Color.Blue;
            this.myTextBox1.Location = new System.Drawing.Point(98, 38);
            this.myTextBox1.Name = "myTextBox1";
            this.myTextBox1.ReadOnly = true;
            this.myTextBox1.Size = new System.Drawing.Size(132, 20);
            this.myTextBox1.TabIndex = 33;
            // 
            // txtExecuted
            // 
            this.txtExecuted.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.txtExecuted.ForeColor = System.Drawing.Color.Blue;
            this.txtExecuted.Location = new System.Drawing.Point(98, 37);
            this.txtExecuted.Name = "txtExecuted";
            this.txtExecuted.ReadOnly = true;
            this.txtExecuted.Size = new System.Drawing.Size(132, 20);
            this.txtExecuted.TabIndex = 33;
            // 
            // txtSourceText
            // 
            this.txtSourceText.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.txtSourceText.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.txtSourceText.ForeColor = System.Drawing.Color.Blue;
            this.txtSourceText.Location = new System.Drawing.Point(98, 216);
            this.txtSourceText.Multiline = true;
            this.txtSourceText.Name = "txtSourceText";
            this.txtSourceText.Size = new System.Drawing.Size(547, 209);
            this.txtSourceText.TabIndex = 33;
            // 
            // txtSource
            // 
            this.txtSource.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.txtSource.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.txtSource.ForeColor = System.Drawing.Color.Blue;
            this.txtSource.Location = new System.Drawing.Point(98, 190);
            this.txtSource.Name = "txtSource";
            this.txtSource.Size = new System.Drawing.Size(516, 20);
            this.txtSource.TabIndex = 33;
            // 
            // cboSourceType
            // 
            this.cboSourceType.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.cboSourceType.FormattingEnabled = true;
            this.cboSourceType.Location = new System.Drawing.Point(98, 138);
            this.cboSourceType.Name = "cboSourceType";
            this.cboSourceType.Size = new System.Drawing.Size(516, 21);
            this.cboSourceType.TabIndex = 39;
            this.cboSourceType.SelectedIndexChanged += new System.EventHandler(this.cboSourceType_SelectedIndexChanged);
            // 
            // cboConnectionStrings
            // 
            this.cboConnectionStrings.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.cboConnectionStrings.FormattingEnabled = true;
            this.cboConnectionStrings.Location = new System.Drawing.Point(98, 165);
            this.cboConnectionStrings.Name = "cboConnectionStrings";
            this.cboConnectionStrings.Size = new System.Drawing.Size(516, 21);
            this.cboConnectionStrings.TabIndex = 39;
            // 
            // frmSQLItem
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(657, 437);
            this.Controls.Add(this.cboConnectionStrings);
            this.Controls.Add(this.cboSourceType);
            this.Controls.Add(this.btnBrowse);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.tsMain);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label7);
            this.Controls.Add(this.label6);
            this.Controls.Add(this.label5);
            this.Controls.Add(this.label8);
            this.Controls.Add(this.label9);
            this.Controls.Add(this.txtRevision);
            this.Controls.Add(this.txtIndex);
            this.Controls.Add(this.txtExpired);
            this.Controls.Add(this.myTextBox1);
            this.Controls.Add(this.txtExecuted);
            this.Controls.Add(this.txtSourceText);
            this.Controls.Add(this.txtSource);
            this.Name = "frmSQLItem";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
            this.Text = "SQLItem";
            this.Load += new System.EventHandler(this.frmSQLItem_Load);
            this.tsMain.ResumeLayout(false);
            this.tsMain.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        private void Save(bool apply)
        {
            this.UpdateObject();
            StringBuilder builder = this.sqlitem.SaveOrUpdate();
            if (builder.Length > 0)
            {
                MessageBox.Show(builder.ToString());
            }
            else if (!apply)
            {
                base.Close();
            }
        }

        private void UpdateObject()
        {
            this.sqlitem.index = this.txtIndex.Text.IntValueOrZero();
            this.sqlitem.revision = this.txtRevision.Text.IntValueOrZero();
            this.sqlitem.sourcetype = this.cboSourceType.SelectedValue.StringValueOrEmpty();
            if (this.sqlitem.sourcetype == "sqlfile")
            {
                this.sqlitem.source = this.txtSource.Text;
            }
            else if (this.sqlitem.sourcetype == "sourcetext")
            {
                this.sqlitem.source = this.txtSourceText.Text;
            }
            this.sqlitem.connectionstringid = this.cboConnectionStrings.SelectedValue.IntValueOrZero();
        }

        private void UpdateViewer()
        {
            this.btnExpire.Text = !this.sqlitem.expired.HasValue ? "Expire" : "Un-Expire";
            this.txtExecuted.Text = this.sqlitem.executed.StringValueOrEmpty();
            this.txtExpired.Text = this.sqlitem.expired.StringValueOrEmpty();
            this.txtIndex.Text = this.sqlitem.index.ToString();
            this.txtRevision.Text = this.sqlitem.revision.StringValueOrEmpty();
            if (this.sqlitem.sourcetype == "sqlfile")
            {
                this.txtSource.Text = this.sqlitem.source;
            }
            else if (this.sqlitem.sourcetype == "sourcetext")
            {
                this.txtSourceText.Text = this.sqlitem.source;
            }
            this.cboSourceType.DataSource = Common.GetSourceTypeDataTable();
            this.cboSourceType.DisplayMember = "name";
            this.cboSourceType.ValueMember = "id";
            this.cboSourceType.SelectedValue = this.sqlitem.sourcetype;
            this.cboConnectionStrings.DataSource = Common.GetConnectionStringDataTable(this.sqlitem.parentid);
            this.cboConnectionStrings.DisplayMember = "name";
            this.cboConnectionStrings.ValueMember = "id";
            this.cboConnectionStrings.SelectedValue = this.sqlitem.connectionstringid;
        }

        public void View(UIHandler puiHandler, SQLItem pSQLItem)
        {
            this._uiHandler = puiHandler;
            this.sqlitem = pSQLItem;
            this.UpdateViewer();
            base.ShowDialog();
        }

        public int id
        {
            get
            {
                return this.sqlitem.id;
            }
        }

        private void frmSQLItem_Load(object sender, EventArgs e)
        {

        }
    }
}

